package behavior.observer.java;

import lombok.Data;

import java.util.Observable;
import java.util.Observer;

/**
 * 战争观察者
 */
@Data
public class WarfareObserver implements Observer {

    /**
     * 观察者名称
     */
    private String name;

    private Integer solidersNum;


    public WarfareObserver(String name) {
        this.name = name;
    }

    public WarfareObserver(String name, Integer solidersNum) {
        this.name = name;
        this.solidersNum = solidersNum;
    }

    @Override
    public void update(Observable o, Object arg) {
        System.out.println(name + "收到消息：" + arg);
        if (this.solidersNum <= 0) {
            System.out.println(name + "战争结束");
            o.deleteObserver(this);
        }
        if (arg.equals(WarfareSubject.ADD)) {
            ++solidersNum;
            System.out.println(name + "士兵数:" + solidersNum);
        } else if (arg.equals(WarfareSubject.DELETE)) {
            --solidersNum;
            System.out.println(name + "士兵数:" + solidersNum);
        }
    }

}
